package main.chapter3

import java.lang.IllegalArgumentException

//接口，描述的一些协议，也就是更加抽象的层面
interface InputDevice {
    fun input(event: Any)
}

interface USBInputDevice : InputDevice

interface BLEInputDevice : InputDevice

interface OpticalMouse

//抽象类，可以理解为半成品，这里实现了USBInputDevice,OpticalMouse的接口
abstract class USBMouse(val name: String) : USBInputDevice, OpticalMouse {
    override fun input(event: Any) {
        println("USB鼠标")
    }


    override fun toString(): String {
        return name
    }
}

//具体的类，可以理解为成品，这边继承抽象类USBMouse
class LogitechMouse : USBMouse("罗技鼠标") {

}

class Computer {
    private fun addUseInputDevice(usbInputDevice: USBInputDevice) {
        //插入use输入设备
        println("add usb input device $usbInputDevice")
    }

    private fun addBLEInputDevice(bleInputDevice: BLEInputDevice) {
        //插入蓝牙设备
        println("add ble input device $bleInputDevice")
    }

    fun addInputDevice(inputDevice: InputDevice) {
        when (inputDevice) {
            is USBInputDevice -> addUseInputDevice(inputDevice)
            is BLEInputDevice -> addBLEInputDevice(inputDevice)
            else -> {
                throw IllegalArgumentException("不支持该设备接口")
            }
        }
    }
}

fun main(args: Array<String>) {
    val computer = Computer()
    val usbMouse = LogitechMouse()
    computer.addInputDevice(usbMouse)
}